#ifndef WBSECSVC_4_WAY_HANDSHAKE_INC__
#define WBSECSVC_4_WAY_HANDSHAKE_INC__

//#include "EapolKey.h"
//#include "sysdef.h"



//*********************************************************************
//Authentication Defintion
//*********************************************************************
//Added bty WangJS 2005.8.26
//To define the length and the type of Key
#define KEYTYPE_WEP40                       0
#define KEYTYPE_WEP104                      1
#define KEYTYPE_TKIP                        2 // 128 bit key
#define KEYTYPE_AES_CCMP                    3 // 128 bit key

#define OPEN_AUTH							0
#define SHARE_AUTH							1
#define ANY_AUTH							2
#define WPA_AUTH							3	//for WPA
#define WPAPSK_AUTH							4
#define WPANONE_AUTH						5

//Added by WangJS 2005.11.16
#define PRFSize                             6 + 6 + 32 + 32


//********************************************************************
//The struct is defined to add the privacy key
//Adde by WangJS 2005.9.20
//********************************************************************
typedef UCHAR MAC_ADDRESS[6];
//typedef unsigned char KEY_RSC[8];

typedef struct _AP_802_11_KEY
{
  ULONG       Length;
  ULONG       KeyIndex;
  ULONG       KeyLength;
  MAC_ADDRESS BSSID;
  //ULONGLONG   KeyRSC;
  ULONG64     KeyRSC;
  UCHAR       KeyMaterial[128];
} AP_802_11_KEY, *PAP802_11_KEY;


//Added by WangJS 2005.11.29

typedef struct _HEX_802_11_KEY
{
	UINT	   KEYLength;
	UCHAR	   KEYMaterial[128];
} HEX_802_11_KEY, *PHEX_802_11_KEY;
//*********************************************************************
///////////////////////////////////////////////////////////////////////
#define ReSendMaxNumber 2            //To define the value of re-send
								//Added by WangJS 2005.10.12
///////////////////////////////////////////////////////////////////////
//To define the meaning block
///////////////////////////////////////////////////////////////////////

typedef UCHAR Block8[8];
typedef UCHAR Block16[16];
typedef UCHAR Block24[24];
typedef UCHAR Block32[32];
typedef UCHAR Block64[64];


typedef UCHAR MACAddress[6];

//Added by WangJS 2005.12.8
#define MACAddressLength 6
///////////////////////////////////////////////////////////////////////
//The structure is defined Station Authentication state
//If the Station is associated  status, the station must process the 
//4-way handshake status
///////////////////////////////////////////////////////////////////////

enum {
	STA_DISASSOC=0,
	STA_ASSOCIATED_COMPLETION,
	STA_4WAY_HANDSHAKE
	
};

//To define the type of install key, added by WangJS 2005.9.29
enum {
	InstallPairwiseKey=0,
	InstallGroupKey
};

//To define the enumeration of 4-way
enum {
	FWayStep1=1,
	FWayStep2,
	FWayStep3,
	FWayStep4,
	GHSStep1,
	GHSStep2,
	//The states is for station
	STA_GHSSTtep2,
	STA_MAX_COUNT
};

///////////////////////////////////////////////////////////////////////
//To define the EAPoL Frame
typedef struct  _EAPoL_PACKET{
	UINT  TotalSize;
	UINT EAPoLBuffer[4096];
}__ALIGN_PACK__ EAPoL_PACKET,pEAPoL_PACKET;
///////////////////////////////////////////////////////////////////////
//typedef void (*OnStepFn)(STATION* STA, PEAP_HEADER EapolMessage, UINT MessageSize);

typedef struct _PairMasterKey{
    Block8          m_ReplayCounter;
    MACAddress      m_AA;                //To retrieve the Authentication address
    Block32         m_ANonce;               //Nonce gnerated from Authentication
    MACAddress      m_SA;
    Block32         m_SNonce;
    Block16         m_KeyIV;
    Block16         m_KeyMIC;
    Block8          m_KeyRSC;
    Block32         m_pmk;
    Block16         m_mk;
    Block16         m_ek;
    Block16         m_tk1;
    Block16         m_tk2;
    Block32         m_KeyNonceSeed;

//	UCHAR   m_OnStep;
	USHORT   m_OnStep;

//	EAPoL_PACKET    EAPoL_Packet;

}__ALIGN_PACK__ PairMasterKey, *pPairMasterKey;

////////////////////////////////////////////////////////////////////////////
//To retrieve information of the group key
//Added by WangJS 2005.10.7
typedef struct _GroupMasterKey{
	Block32         m_GNonce;               //Nonce generate by Group Handshake, added by WangJS 2005.9.28
	Block16         m_gtk1;
    Block16         m_gtk2;
}__ALIGN_PACK__ GroupMasterKey, *pGroupMasterKey;

////////////////////////////////////////////////////////////////////////////
//To retrieve information of the Station Status

typedef struct _STATION{
	MACAddress     STA_MAC_ADDRESS;      //To identify the STA 
//	BOOL	       IsConnected;          //To retrieve the Connect status
	PairMasterKey  PMK;
	GroupMasterKey GMP;
//	UCHAR		   ReSendNumber;         //Added by WangJS 2005.10.12
//	UINT		   TimeStamp;
    
}__ALIGN_PACK__ STATION, *pSTATION;


/////////////////////////////////////////////////////////////////////////////
//To define the variable
/////////////////////////////////////////////////////////////////////////////
#define  pGlobal_STA_Wpa (&(Adapter->Global_STA_Wpa))
#define  pSTA (&(Adapter->Global_STA_Wpa.WPA_STA))
//**************************************************************************
    enum {
        WLAN_EID_RSN_WPA    = 0xDD,
        WLAN_EID_RSN_WPA2   = 0x30, 
    };

    enum {
    		WLAN_RSN_VERSION    = 0x0001,
	};

    enum {
        CIPHER_SUITE_NONE   = 0,
        CIPHER_SUITE_WEP40  = 1,
        CIPHER_SUITE_TKIP   = 2,
        CIPHER_SUITE_WRAP   = 3,
        CIPHER_SUITE_CCMP   = 4,
        CIPHER_SUITE_WEP104 = 5,
    };

    enum {
        AUTH_KEY_MGT_SUITE_RESERVED     = 0,
        AUTH_KEY_MGT_SUITE_UNSPECIFIED  = 1,
        AUTH_KEY_MGT_SUITE_NONE         = 2,
    };
 



//***********************************************************************
// To start the 4 Way Handshake
//***********************************************************************
  //void Start4WayHandshake(unsigned short StaState, MACAddress ReceievedStaMACAddress, unsigned int PacketSize, void* Packet);
   VOID Start4WayStateMachine(PVOID DeviceHandle, UINT PacketSize, PVOID Packet , UINT SendPacketSize, PVOID SendPacket);
//***********************************************************************
// EAPoL packet process
//***********************************************************************
    VOID AP_PasswordHash(PVOID DeviceHandle, /*const */UCHAR* Password, Block32* PMK);
    //Added by WangJS 2005.12.12
    VOID AP_PrePasswordHash(PVOID DeviceHandle, UCHAR* SSID, UCHAR SSIDLength);
    

//***********************************************************************
//The 4Way Handshake
//***********************************************************************
    	
    //Clear all state
    VOID FourWayClear(PVOID DeviceHandle);
    // Pairwise key generation functions
    VOID FourWayInit(PVOID DeviceHandle);

    VOID AP_OnStep2(PVOID DeviceHandle, STATION* sta, PVOID Eap, UINT MessageSize);

    //void AP_OnStep3(STATION* STA, void* Eap, unsigned int MessageSize);
    VOID AP_OnStep4(PVOID DeviceHandle, STATION* sta, PVOID Eap, UINT MessageSize);
       
	//For Group handshake of Station, Added by WangJS 2005.11.11
    VOID STA_OnGStep2(PVOID DeviceHandle, STATION* sta, PVOID Eap, UINT MessageSize);
	//The function can generate the pairwise key and group key
    VOID GenerateANonce(Block32* KeyNonceSeed);

	VOID GeneratePairwisePRF(UCHAR* PairwisePRF,
							 Block32*		  pmk, 
							 MACAddress*	  AA, 
							 MACAddress*	  SA, 
							 Block32*		  ANonce, 
							 Block32*		  SNonce);

	//Generate GroupPRF, added by WangJS 2005.9.28
	    
	//Added by WangJS 2005.11.11, in order to fill the rsn element of station
	UINT Supplicat_FillRSN(PVOID DeviceHandle, UCHAR* rsn, UINT rsn_len);

    UINT BuildEapolKeyMessage(PVOID DeviceHandle, UCHAR* Buffer, UINT BufferSize,
							 MACAddress* DA, MACAddress* SA, 
							 UINT KeyDescVer, BOOL bSecure, BOOL bMIC, BOOL bResponse, 
    
    BOOL bPairwise, BOOL bInstall, UINT KeyIndex,Block8* ReplayCount, Block8* KeyRSC, 
							 Block32* KeyNonce, Block16* MK, Block16* KeyIV,
							 UCHAR* KeyData, UINT KeyDataSize);

    BOOL KeyMICIsValid(PVOID DeviceHandle, PEAP_HEADER Eap, UINT Len,  Block16* MK, Block16* m_mk);

    BOOL Is8021xPacket(USHORT Type);
	
	//Added by WangJS 2005.9.27
    BOOL IsEAPoL_KEY_FRAME(PVOID Eap, UINT Len);
	//////////////////////////////////////////////////////////////
	//Group Key Function
	//Added by WangJS 2005.9.28
	//////////////////////////////////////////////////////////////
	VOID GenerateGTK(GroupMasterKey* GMK);
	//////////////////////////////////////////////////////////////
	//VOID InstallKey(PVOID DeviceHandle, MACAddress* STA_MAC_Address, Block16* Key1,Block16* Key2, BOOL KeyType);
	VOID InstallKey(PVOID DeviceHandle, MACAddress* STA_MAC_Address, Block16* Key1,Block16* Key2, BOOL KeyType, USHORT KeyIndex);
	
	//Added by WangJS 2005.11.28
	VOID FourWay_Get_Encryption_Status(PVOID DeviceHandle, UCHAR *EncryptStatus);
	//random function
	VOID srand(ULONG seed);
	UINT rand(void);

	//cpu word
	VOID CpuWord(USHORT* w);
	UCHAR Compare(PVOID Byte1, PVOID Byte2, UINT ByteSize, UCHAR ComStr);
	
	//Added by WangJS 2005.12.23
	VOID TimeStamp(PVOID TagString);
	//Added by WangJS 2006.8.11
	//To check the packet of EAPoL frame is pairwise or group key
	UINT CheckEAPType(PVOID DeviceHandle,PVOID RecvPacket);
	//Added by WangJS 2006.8.22
	UCHAR CharToBin(UCHAR Character);

#endif // WBSECSVC_4_WAY_HANDSHAKE_INC__
// end of file


